]> dgit.raspbian.org Git - gnome-shell.git/commitdiff
params: Simplify code
authorFlorian Müllner <fmuellner@gnome.org>
Tue, 2 Jul 2019 03:24:29 +0000 (05:24 +0200)
committerFlorian Müllner <florian.muellner@gmail.com>
Fri, 5 Jul 2019 11:32:31 +0000 (11:32 +0000)
Standard javascript now has Object.assign() which is very similar to
Params.parse(), except that the latter by default disallows "extra"
parameters. We can still leverage the standard API by simply
implementing the error check, and then call out to Object.assign()
for the actual parameter merging.

https://gitlab.gnome.org/GNOME/gnome-shell/merge_requests/612

js/misc/params.js

index 04691ba6a4f2db5ed6e6261819865a27036be0de..0e3af95323610437f55930dec7b42ffce7bd9c99 100644 (file)
 //
 // Return value: a new object, containing the merged parameters from
 // @params and @defaults
-function parse(params, defaults, allowExtras) {
-    let ret = {}, prop;
-
-    if (!params)
-        params = {};
-
-    for (prop in params) {
-        if (!(prop in defaults) && !allowExtras)
-            throw new Error('Unrecognized parameter "' + prop + '"');
-        ret[prop] = params[prop];
-    }
-
-    for (prop in defaults) {
-        if (!(prop in params))
-            ret[prop] = defaults[prop];
+function parse(params = {}, defaults, allowExtras) {
+    if (!allowExtras) {
+        for (let prop in params)
+            if (!(prop in defaults))
+                throw new Error(`Unrecognized parameter "${prop}"`);
     }
 
-    return ret;
-}
\ No newline at end of file
+    return Object.assign(defaults, params);
+}